package com.wk.springboot.ttl.context;

import com.alibaba.ttl.TransmittableThreadLocal;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class InheritableThreadLocalExample2 {

    private static InheritableThreadLocal<String> inheritableThreadLocal = new InheritableThreadLocal<>();

    public static void main(String[] args) throws InterruptedException {
        inheritableThreadLocal.set("Parent Thread Value");

        ExecutorService executorService = Executors.newFixedThreadPool(1);

        // 提交任务到线程池
        for (int i = 0; i < 4; i++) {
            Thread.sleep(1000);
            int finalI = i;
            executorService.execute(() -> {
                System.out.println("Thread [" + Thread.currentThread().getName() + "] InheritableThreadLocal Value: " + inheritableThreadLocal.get());
                inheritableThreadLocal.set("Parent Thread Value"+ finalI);
            });
            System.out.println("main Thread  InheritableThreadLocal Value: " + inheritableThreadLocal.get());
        }

        // 关闭线程池
        executorService.shutdown();
    }
}
